<?php
/**
 *  等级配置表
 * ds
 * 2020.08.13
 */
namespace App\Models\MainDB;

use App\Services\Tools\RedisService;
use Illuminate\Database\Eloquent\Model;

class GradeConfig extends Model{

    protected $table = 'ln_grade_config';     //数据库内完整表名

    protected $primaryKey = 'id';    //主键名

    protected $fillable = [
        'id',      // 主键id
        'grade',         //  等级
        'experience',         //  经验 
        'head_picture',         //  头像框图片
        'name',
        'grade_big_id',
        'reward_id',
        'screen_img',
    ];

    public $timestamps = false;

    public function reward() {
        return $this->hasOne("App\Models\MainDB\Reward", "id", "reward_id");
    }

    //获取阶段等级信息
    public function getGradeIcon($id) {
        $grade_list = RedisService::getGradeConfig();
        $max = array_slice($grade_list, -1, 1);
        if ($id > $max[0]['id']) {
            return $max[0]['head_picture'];
        }

        return $grade_list[$id]['head_picture'] ?? '';
    }

    public function getGradeConfig($id)
    {
        $grade_list = RedisService::getGradeConfig();
        $max = array_slice($grade_list, -1, 1);
        if ($id > $max[0]['id']) {
            return $max[0] ?? [];
        }

        return $grade_list[$id] ?? [];
    }
}